package com.bw.rjx.controller;

import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;


import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 人气指标API控制器
 */
@RestController
public class PopularityController {

    private Jedis jedis = new Jedis("hadoop102", 6379);

    /**
     * 获取店铺的所有人气指标
     */
    @GetMapping("/store/{shop_id}")
    public Map<String, Object> getStorePopularity(@PathVariable String shop_id) {
        Map<String, Object> result = new HashMap<>();
        
        // 匹配该店铺的所有指标
        Set<String> keys = jedis.keys("shop_id:" + shop_id + ":*");
        
        for (String key : keys) {
            Map<String, String> metric = jedis.hgetAll(key);
            result.put(key, metric);
        }
        System.out.println(keys);
        System.out.println("result = " + result);
        return result;
    }

    /**
     * 获取特定商品的人气指标
     */
    @GetMapping("/store/{storeId}/product/{productId}")
    public Map<String, Object> getProductPopularity(
            @PathVariable String storeId, 
            @PathVariable String productId) {
        
        Map<String, Object> result = new HashMap<>();
        Set<String> keys = jedis.keys("shop_id:" + storeId + ":sku_id:" + productId + ":*");
        
        for (String key : keys) {
            Map<String, String> metric = jedis.hgetAll(key);
            result.put(key, metric);
        }
        
        return result;
    }
    
    /**
     * 获取实时汇总数据
     */
    @GetMapping("/realtime/summary")
    public Map<String, Object> getRealtimeSummary() {
        // 实现实时汇总逻辑
        return new HashMap<>();
    }
}
